package com.jeespring.leetcode.array;

import java.util.Arrays;

/**
 * @Title: SingleNumber
 * @Description:
 * @Author 张朝阳
 * @Date 2020/12/26 16:29
 */
public class SingleNumber {
    public static void main(String[] args) {
        int[] nums = {2,2,33,1,33,6,6};
        int single = single(nums);
        System.out.println(single);
    }

    public static int single(int[] nums){
        int num = nums[0];
        for (int i = 0; i < nums.length; i++) {
            boolean flag = false;
            for (int j = 0; j < nums.length; j++) {
                if (i == j){
                    continue;
                }
                if (nums[i] == nums[j]){
                    flag = true;
                    break;
                }
            }
            if (!flag){
                num = nums[i];
            }
        }
        return num;
    }

    public static int singleNumber(int[] nums) {
        int num = 0;
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            num = i % 2 == 0 ? num + nums[i] : num - nums[i];
        }
        return num;
    }
}
